<?php
namespace Home\Controller;

use Think\Controller;

//
class LoginController extends Controller
{

	function _initialize(){
        // TODO
    }

    /**
     * 登录页面
     */
	public function index(){
		$this->display();
	}


    /**
     * 注册页面
     */
    public function register(){
		$this->display();
    }


    /**
     * 处理登录方法
     */
    public function doLogin()
    {
        // TODO
        session('user', true);
        $phone=$_POST['phone'];
        $password=$_POST['password'];
        $user=M('yh_system_users');
        if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$phone)){//验证通过
            $status=$user->where("U_LOGINNAME='".$phone."' and U_PASSWORD='".md5($password)."'")->find();
            // $status=$user->where("U_LOGINNAME='".$phone."' and U_PASSWORD='".$password."'")->find();
            if($status){
                session('user',$phone);
                $data['status'] = 1;
                $data['info'] = '登陆成功';
            }else{
                $data['status'] = -1;
                $data['info'] = '密码错误';
            }
        }else{//手机号码格式不对
            $data['status'] = -2;
            $data['info'] = '手机号格式不对';
        }
        $this->ajaxReturn($data,'JSON');
    }

    /**
     * 处理注册信息
     */
    public function doRegister()
    {
        // TODO
        $user=M('yh_system_users');
        $phone=$_POST['phone'];
        $verify=$_POST['verify'];
        $code=session('code');
        if($code!=$verify){
            $info['status'] = -1;
            $info['info'] = '验证码错误';
        }else{
            if(empty($_POST['password'])){
                $info['status'] = -2;
                $info['info'] = '密码不能为空';
            }else{
                $data['U_PASSWORD']=$_POST['password'];
                $data['U_NAME']=$phone;
                $data['U_LOGINNAME']=$phone;
                $data['U_NOWTIME']=date("Y-m-d H:i:s", time());
                $data['U_RULEID']=1;
                $status=$user->save($data);
                if($status){
                    $info['status'] = 1;
                    $info['info'] = '注册成功';
                }else{
                    $info['status'] = -2;
                    $info['info'] = '注册失败';
                }
            }
        }
        $this->ajaxReturn($info,'JSON');
    }

    /**
     * 获取验证码
     */
    public function getVerify()
    {
        $phone = I('phone');
        if(preg_match("/^1(3|5|7|8)[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$phone)){//验证通过
            $code = mt_rand("111111", "999999");
            session('code',$code);
            if($this->send($phone,$code)){
                $data['status'] = 1;
                $data['info'] = '验证码已发送';
            }else{
                $data['status'] = -1;
                $data['info'] = '发送失败';
            }
        }else{//手机号码格式不对
            $data['status'] = -2;
            $data['info'] = '手机号格式不对';
        }
        $this->ajaxReturn($data,'JSON');
    }
    /**
     * 验证短信
     * @param 手机号 $phone
     * @param 验证码 $code
     */
    public function send($phone,$code){
        
    }
    /**
     * 用户协议
     */
    public function license()
    {
        // TODO
        $this->display();
    }
}